package com.tjian.fafa.user.base.myNetwork;

import android.content.Context;

import com.tjian.fafa.user.base.commons.GlobalStrings;

import java.net.Socket;
import java.util.Map;

public class NetworkService
{
    private static NetworkService instance;

    public static NetworkService getInstance()
    {
        if (instance == null)
        {
            instance = new NetworkService();
        }
        return instance;
    }

    private boolean isConnected;
    private NetConnect netConnect;

    private ClientListenThread clientListenThread;
    private ClientSendThread clientSendThread;

    private Socket socket;
    private Context context;

    private NetworkService()
    {
        isConnected = false;
    }

    public void onInit(Context context)
    {
        this.context = context;
    }

    public void setupConnection()
    {
        netConnect = new NetConnect();
        netConnect.start();
        try
        {
            netConnect.join();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        if (netConnect == null || !netConnect.connectedOrNot())
        {
            isConnected = false;
            return;
        }
        else
        {
            socket = netConnect.getSocket();
            isConnected = true;
            startListen(this.context);

            if (socket != null)
            {
                //System.out.println("socket不为空。");
            }
            else
            {
                System.out.println("socket为空==========。");
            }
        }
    }

    private void startListen(Context contextParam)
    {
        clientListenThread = new ClientListenThread(contextParam, socket);
        clientListenThread.start();

        clientSendThread = new ClientSendThread();
    }

    public boolean getIsConnected()
    {
        return isConnected;
    }

    public void sendUpload(int type, String sentence)
    {
        sendUpload(type + "");
        sendUpload(sentence);
    }

    public void sendUpload_json_fafa(String type, String sentence)
    {
        sendUpload_json_fafa(type + "");
        sendUpload_json_fafa(sentence);

        //sendUpload_json_fafa(paramMap);
    }

    /*  同步,确保同一时间只有一个send action */
    private synchronized void sendUpload(String buff)
    {
        buff = buff.replace("\n", GlobalStrings.replaceOfReturn);
        if (buff == null || socket == null)
        {
            return;
        }
        clientSendThread.start(socket, buff);
    }

    /*  同步,确保同一时间只有一个send action */
    private synchronized void sendUpload_json_fafa(String buff)
    {
        buff = buff.replace("\n", GlobalStrings.replaceOfReturn);
        if (buff == null || socket == null)
        {
            return;
        }
        clientSendThread.start_json_fafa(socket, buff);
    }

    public void closeConnection()
    {
        try
        {
            if (clientListenThread != null)
            {
                clientListenThread.closeBufferedReader();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            if (socket != null)
            {
                socket.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        socket = null;
        isConnected = false;
    }

}
